from django.shortcuts import render
from django import http
from django.views import View
from .models import BookInfo,HeroInfo
# Create your views here.

class BookView(View):
    def get(self,request):

        # 1. 增加 save
        # book = BookInfo()
        # book.btitle = '盗墓笔记'
        # book.bpub_date = '2005-01-01'
        # book.save()

        # 2. create
        # BookInfo.objects.create(
        #     btitle = "黄河鬼棺",
        #     bpub_date = "2004-01-01"
        # )

        # book = BookInfo.objects.create(
        #     btitle = "盗墓笔记2",
        #     bpub_date = "2004-01-01"
        # )
        # # 英雄
        # hero = HeroInfo.objects.create(
        #     hname = '小哥',
        #     hgender = 0,
        #     hcomment = '死不了',
        #     hbook =book
        # )
        # hero = HeroInfo.objects.create(
        #     hname = '小哥',
        #     hgender = 0,
        #     hcomment = '死不了',
        #     hbook_id = 6
        # )
        books = BookInfo.objects.all()
        context = {
            'name':'模板操作',
            'books': books
        }
        # return http.JsonResponse({'data':hero.hname})
        # return render(request,'index.html', context)
        response = render(request,'index.html',context)
        # 1. 设置cookie
        # response.set_cookie('name', 'ITHEIMA', max_age=3600)
        # 2. 读取cookie
        # cookies = request.COOKIES.get('name')
        # print(cookies)
        # 3. 删除cookie
        response.delete_cookie('name')
        return response
# 增加 save()  ,create()

# 修改 save() ,update()
# from booktest.models import BookInfo,HeroInfo
# book = BookInfo.objects.get(id=5)
# book.btitle = '盗墓笔记0'
# book.save()
# BookInfo.objects.filter(id=7).update(btitle='龙岭迷窟')
# # 删除 delete()
# book = BookInfo.objects.get(id=7)
# book.delete()
# BookInfo.objects.filter(btitle='黄河鬼棺').delete()
# # 查询
# # 获取一个
# BookInfo.objects.get(id=1)
# # 获取多个数据对象
# BookInfo.objects.filter(id=1)
# # 获取所有
# BookInfo.objects.all()
# # 统计个数
# BookInfo.objects.count()
#
# # F对象和Q对象
# # F对象 多属性对比
# # 查询阅读量大于评论量的书
# from django.db.models import F ,Q
# BookInfo.objects.filter(bread__gt=F('bcomment'))
#
#
#
# # Q对象 逻辑运算符  --多条件
# # 查询阅读量大于20，并且编号小于3的图书
# # BookInfo.objects.filter(bread__gt=30,id__lt=3)
# BookInfo.objects.filter(Q(bread__gt=30)&Q(id__lt=3))
# # 查询阅读量大于20 或者编号小于3的
# BookInfo.objects.filter(Q(bread__gt=20)|Q(id__lt=3))
#
#
#
#
# # 聚合查询aggregate --排序 ordder_by
# from django.db.models import Sum, Avg, Count,Max, Min
# # 求图书的总阅读量
# BookInfo.objects.aggregate(Sum('bread'))
# # 求平均值
# BookInfo.objects.aggregate(Avg('bread'))
#
# # 求总数
# BookInfo.objects.count()
#
# # 排序 降 默认是升序
# BookInfo.objects.order_by('id')
# BookInfo.objects.order_by('-id')
# # id小于三降序
# BookInfo.objects.filter(id__lt=3).order_by('id')
#
# # 关联查询
# # ORM 关联查询是不会变得  语法是固定的
# # 通过图书找里面的所有英雄
# # 一对多  1.n_set
# # 查询第三本的 男 英雄有哪些
# book = BookInfo.objects.get(id=3)
# book.heroinfo_set.filter(hgender=1)
#
# # 通过英雄找书
# # 多对一   n.外键属性
# # 查询郭靖属于那本书
# hero = HeroInfo.objects.get(hname='郭靖')
# # 取出数据对象
# hero.hbook
# # 取出图书id
# hero.hbook_id
